package easy

import "workspace/node"

func FindTilt(root *node.TreeNode) int {
	res := 0
	var dfs func(*node.TreeNode) int
	dfs = func(node *node.TreeNode) int {
		if node == nil {
			return 0
		}
		leftSum := dfs(node.Left)
		rightSum := dfs(node.Right)
		res += abs(leftSum - rightSum)
		return leftSum + rightSum + node.Val
	}
	dfs(root)
	return res
}
